package se.chalmers.tda366.crumble.gui.dialogs;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

import se.chalmers.tda366.crumble.core.ICategory;
import se.chalmers.tda366.crumble.core.IFeed;
import se.chalmers.tda366.crumble.core.impl.Category;
import se.chalmers.tda366.crumble.gui.utils.CloseActionListener;
import se.chalmers.tda366.crumble.gui.utils.GuiObserv;
import se.chalmers.tda366.crumble.resources.Constants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConfigureFeedsView extends JPanel{
	
	private JLabel feedNameLabel;
	private JLabel choseFeedLabel;
	private JButton editButton;
	private JComboBox feedsComboBox;
	private JLabel feedCategoryLabel;
	private JLabel addressLabel;
	private JTextField feedNameTextField;
	private JTextField feedAdressTextField;
	private JComboBox categoryComboBox;
	private JButton exitButton;
	private JLabel HeaderLabel;
	private ArrayList<String> feedNameList;
	private List<IFeed> feeds;
	private List<ICategory> categories;
	private URL address;

	public ConfigureFeedsView(List<IFeed> feeds, List<ICategory> categories) {
		this.feeds = feeds;
		this.categories = categories;
		this.initGUI();
		this.initCtrl();
		updateField();
	}
	
	private void initCtrl() {
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					address = new URL(feedAdressTextField.getText());
					GuiObserv.getInstance().fireNotify(((JButton)evt.getSource()).getParent(), Constants.Actions.EDIT_FEED);
				} catch (MalformedURLException e1) {
					JOptionPane.showMessageDialog(null, "Feed adress is invalid");
				}
		}
	});

		exitButton.addActionListener(new CloseActionListener());
	}
	
	public URL getUrl(){
		return address;
	}
	
	public String getNewName(){
		return feedNameTextField.getText();
	}
	
	public IFeed getFeed(){
		return (IFeed) feedsComboBox.getSelectedItem();
	}
	
	public JComboBox getCategoryComboBox(){
		return categoryComboBox;
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
				this.setLayout(thisLayout);
				this.setPreferredSize(new java.awt.Dimension(354, 242));
				this.setSize(new java.awt.Dimension(342, 242));
				{
					feedNameLabel = new JLabel();
					feedNameLabel.setName("feedNameLabel");
					feedNameLabel.setText("Feed Name:");
					feedNameLabel.setAlignmentX(-0.0f);
				}
				{
					HeaderLabel = new JLabel();
					HeaderLabel.setFont(se.chalmers.tda366.crumble.resources.Constants.HEADLINEFONT);
					HeaderLabel.setHorizontalTextPosition(SwingConstants.CENTER);
					HeaderLabel.setHorizontalAlignment(SwingConstants.CENTER);
					HeaderLabel.setName("HeaderLabel");
					HeaderLabel.setText("Configure Feeds");
				}
				{
					choseFeedLabel = new JLabel();
					choseFeedLabel.setName("choseFeedLabel");
					choseFeedLabel.setText("Chose Feed:");
				}
				{
					exitButton = new JButton("Cancel");
					exitButton.setName("exitButton");
				}
				{   
					ComboBoxModel feedsComboBoxModel = 
						new DefaultComboBoxModel(feeds.toArray());
					feedsComboBox = new JComboBox();
					feedsComboBox.setModel(feedsComboBoxModel);
					feedsComboBox.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							feedsComboBoxActionPerformed(evt);
						}
					});

				}
				{
					editButton = new JButton();
					editButton.setName("editButton");
					editButton.setText("Save");
				}
				{
					ArrayList<ICategory> newList = new ArrayList<ICategory>();
					ICategory nullCat = new Category("< Choose here to select new cetegory >");
					newList.add(nullCat);
					newList.addAll(categories);
					ComboBoxModel categoryComboBoxModel = 
						new DefaultComboBoxModel(newList.toArray());
					categoryComboBox = new JComboBox();
					categoryComboBox.setModel(categoryComboBoxModel);
					categoryComboBox.setName("categoryComboBox");
				}
				{
					feedAdressTextField = new JTextField();
				}
				{
					feedNameTextField = new JTextField();
					feedNameTextField.setText("Feed name");
					feedNameTextField.setNextFocusableComponent(feedAdressTextField);
				
				}
				{
					addressLabel = new JLabel();
					addressLabel.setName("addressLabel");
					addressLabel.setText("Feed Address:");
				}
				{
					feedCategoryLabel = new JLabel();
					feedCategoryLabel.setName("feedCategoryLabel");
					feedCategoryLabel.setText("Feed Category:");
				}
				thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
					.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					    .addComponent(HeaderLabel, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
					    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGroup(thisLayout.createSequentialGroup()
					    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 30, GroupLayout.PREFERRED_SIZE)
					    .addGroup(thisLayout.createParallelGroup()
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addComponent(addressLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					            .addGap(6))
					        .addComponent(feedCategoryLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGroup(thisLayout.createSequentialGroup()
					            .addPreferredGap(addressLabel, choseFeedLabel, LayoutStyle.ComponentPlacement.INDENT)
					            .addGroup(thisLayout.createParallelGroup()
					                .addComponent(choseFeedLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(feedNameLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
					    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					    .addGroup(thisLayout.createParallelGroup()
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addComponent(feedsComboBox, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 26, Short.MAX_VALUE))
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addComponent(feedNameTextField, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 26, Short.MAX_VALUE))
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addComponent(feedAdressTextField, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 26, Short.MAX_VALUE))
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addComponent(categoryComboBox, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					            .addGap(0, 26, Short.MAX_VALUE))
					        .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					            .addGap(0, 65, Short.MAX_VALUE)
					            .addComponent(exitButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
					            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					            .addComponent(editButton, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)))
					    .addContainerGap()));
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(HeaderLabel, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(feedsComboBox, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(choseFeedLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(feedNameTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(feedNameLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(feedAdressTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(addressLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(categoryComboBox, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(feedCategoryLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(0, 25, Short.MAX_VALUE)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(exitButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
					    .addComponent(editButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
					.addContainerGap());
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.repaint();
	}
	
	private void updateField() {
		IFeed selected = (IFeed) feedsComboBox.getSelectedItem();
		feedAdressTextField.setText(selected.getSource());
		feedNameTextField.setText(selected.getName());		
	}
	
	private void feedsComboBoxActionPerformed(ActionEvent evt) {
		updateField();
	}
}